package com.zte.claa.inficombo.csa.app.config;

import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;


@Configuration 
public class RedisConfig {
	// redis 消息发布者
	/**
	 *  发布消息： template.convertAndSend( topic.getTopic(), msg);
	@Bean
    MyPublisher redisPublisher(RedisConnectionFactory factory) {
        return new MyPublisher(redisTemplate(factory), testTopic());
    }

    @Bean
    ChannelTopic testTopic() {
        return new ChannelTopic( "pubsub:queue" );
    }
    */

    /**
     * 注册监听器: container.addMessageListener(messageListener(), testTopic());
    @Bean
    MessageListenerAdapter messageListener() {
        return new MessageListenerAdapter( new MyMessageListener() );
    }
    */
	
	// redis String操作模板（封装了redis的各类操作）
    @Bean
    StringRedisTemplate redisStringTemplate(RedisConnectionFactory factory){
    	 return new StringRedisTemplate(factory);
    }
    
    // redis Object操作模板（封装了redis的各类操作）
    @Bean
    RedisTemplate<String, Object> redisObjectTemplate(RedisConnectionFactory factory){
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
        return template;  
    }
    
    @Bean
    ThreadPoolTaskExecutor redisTaskExecutor() {
    	// 创建线程池，避免RedisMessageListenerContainer每次监听到消息都创建线程.
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(8);
        taskExecutor.setMaxPoolSize(32);
        taskExecutor.setKeepAliveSeconds(20);
        taskExecutor.setQueueCapacity(20000);
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        taskExecutor.setThreadNamePrefix("Redis-subscribe-thread");
        return taskExecutor;
    }

    // redis listener container.
    @Bean
    RedisMessageListenerContainer redisContainer(RedisConnectionFactory factory, ThreadPoolTaskExecutor taskExecutor) {
        final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);        
        container.setTaskExecutor(taskExecutor);
        // 各模块动态添加监听器.
        // container.addMessageListener(messageListener(), testTopic());
        return container;
    }
	  
}
